<template>
  <el-dialog
    :width="800"
    custom-class="cron-box-dialog-first"
    v-model="dialogVisible"
    v-if="dialogVisible"
  >
    <template #title>
      <div style="font-size:16px;color:#666666;">
        cron表达式生成器
      </div>
    </template>

    <el-tabs tab-position="left" v-model="activeName">
      <el-tab-pane label="快捷生成" name="first">
        <CronDefault
          ref="CronDefaultRef"
          v-if="activeName == 'first'"
        ></CronDefault>
      </el-tab-pane>
      <el-tab-pane label="自定义生成" name="second">
        <CronExc ref="CronExcRef" v-if="activeName == 'second'"></CronExc>
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <div>
        <el-button size="mini" @click="confirmClick" type="primary"
          >确定</el-button
        >
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { reactive, toRefs, ref } from "vue";
import CronExc from "../cron-custom/index";
import CronDefault from "../cron-default/index";
export default {
  components: {
    CronExc,
    CronDefault
  },
  setup(props, ctx) {
    const CronDefaultRef = ref(null);
    const CronExcRef = ref(null);
    const state = reactive({
      dialogVisible: false,
      activeName: "first",
      result: "" // 最终的结果表达式
    });
    // 确定生成表达式
    const confirmClick = () => {
      if (state.activeName == "first") {
        state.result = CronDefaultRef.value.cronExpression;
      } else {
        state.result = CronExcRef.value.cronExpression;
      }
      ctx.emit("cronResult", state.result);
      state.dialogVisible = false;
    };
    return {
      CronDefaultRef,
      CronExcRef,
      ...toRefs(state),
      confirmClick
    };
  }
};
</script>
<style lang="css">
.cron-box-dialog-first {
  width: 700px;
}
.cron-box-dialog-first .el-dialog__header {
  padding: 10px;
  text-align: center;
}
.cron-box-dialog-first .el-dialog__header .el-dialog__headerbtn {
  top: 12px;
  right: 8px;
}
.cron-box-dialog-first .el-dialog__body {
  padding: 0 10px;
}
.cron-box-dialog-first .el-dialog__footer {
  padding: 10px;
  text-align: center;
}
</style>
